import {Article} from "@/class/Article";

const state={
    articleList:[],
    count:0,
    currentPage:1,
    pageSize:5
}
const mutations={
    GETARTICLEBYTOKEN(state,articleList){
        state.articleList=articleList;
    },
    GETARTICLECOUNT(state,count){
        state.count=count
    },
    GETARTICLEBYPAGE(state,articleListByPage){
        state.articleList=articleListByPage
    }
}
const actions={
    async getArticleCount({commit,dispatch}) {
        const count = await Article.getArticleCount();
        commit("GETARTICLECOUNT",count)
        if(state.count!==0&&Math.ceil(state.count/state.pageSize)<state.currentPage){
            state.currentPage=state.currentPage-1
            dispatch("getArticleByPage")
        }
    },
    /**
     * 分页获取数据
     * @param commit
     * @param state
     * @param currentPage 可选
     * @param pageSize 可选
     * @returns {Promise<*>}
     */
    async getArticleByPage({commit,state,dispatch}, currentPage) {
        if(currentPage!==undefined&&currentPage!=="")
            state.currentPage=currentPage;
        dispatch("getArticleCount");
        const result = await Article.getArticleByPage(state.currentPage, state.pageSize)
        commit('GETARTICLEBYPAGE', result)
    }
}
const getters={

}
export default {
    state,mutations,actions,getters
}